/*************************************************************************
	> File Name: channel.h
	> Author: xuwenlong
	> Mail: myxuan475@126.com 
	> Created Time: 2018年12月04日 星期二 09时25分58秒
 ************************************************************************/
#ifndef __CHANNEL_H__
#define __CHANNEL_H__
#include <transport/transport.h>
#include "acquisitor.h"

class Acquisitor;
class Linkage;
class Collector;
class Template;
class Channel;
typedef utils::SharedPtr<Acquisitor> tAcquisitor;
typedef utils::SharedPtr<Template> tTemplate;
typedef utils::SharedPtr<Channel> tChannel;

class Channel:public TimerTask,public utils::RefrenceObj
{
public:
    Channel(const transmgr_t &trans);
    virtual ~Channel();

    virtual void RunTimer();

    bool clearData();
    S32 commitData(S32 index);

    void Start(time_t &t);
    void Stop();
    bool IsEqual(const transmgr_t &trans);
    void StartAcquisitor();
    void StopAcquisitor();
    tAcquisitor GetAcquisitor(const deviceinfo_t& devinfo);
    tConnection GetConnector();
    vector<tAcquisitor> GetAllAcquisitors();

    void AcquireList(Json::Value &result);
    double dailyMemory();//KB

private:
    void initAcquisitor();

    string getChnName(const transmgr_t &trans);

    BOOL updateConnector(S32 sample);

private:
    class CommitTimer
    {
    public:
        CommitTimer(Timer *timer_,int sample_) {
            timer = timer_;
            sample = sample_;
        }

        ~CommitTimer() {
            time_t now = time(NULL);
            S64 secs = (S64)(60*sample);

            if (secs <= 0) {
                LOG_ERR("sample is %lld second",secs);
                return;
            }

            //修正时间
            S64 amend = (secs+1)/5;
            S64 wait = secs-((S64)(now+amend))%secs+amend;

            if (wait <= 120)
                wait = 1;
            else
                wait -= 120;
            LOG_INFO("time:%s,wait %lld s",
                     utils::timetostr(time(NULL)).c_str(),
                     wait);
            timer->set_timeout(wait*1000);
        }
    private:
        Timer *timer;
        int  sample;
    };
private:
    transmgr_t m_trans;
    Timer *                 m_pTimer;

    bool                    m_bStop;

    //采集器
    vector<tAcquisitor>    m_listAcquisitor;
};

#endif
